<?php

namespace App;

use App\Models\AdminUser;
use App\Models\Department;

class BookApply extends BaseModel
{

    public function user()
    {
        return $this->belongsTo(AdminUser::class, 'user_id');
    }

    public function department()
    {
        return $this->belongsTo(Department::class, 'department_id');
    }

    public function transaction()
    {
        return $this->hasOne(BookSale::class, 'data_id');
    }

    public function addSale(array $data)
    {
        $this->transaction()->updateOrCreate([], [
            'name'           => $this->name,
            'sn'             => $this->register_no,
            'department_id'  => $this->department_id,
            'user_id'        => $this->user_id,
            'buyer'          => data_get($data, 'buyer'),
            'contact_person' => data_get($data, 'contact_person'),
            'contact_mobile' => data_get($data, 'contact_mobile'),
            'sale_amount'    => data_get($data, 'amount'),
            'sale_type'      => data_get($data, 'type'),
            'sale_date'      => data_get($data, 'date'),
        ]);
        $this->status = 2;
        $this->save();
    }
}
